package com.zhss.seckill.page;

import com.alibaba.fastjson.JSONObject;
import com.zhss.seckill.page.freemarker.FreemarkerHelper;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class BootListener implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
//        RocketMQConsumer.setConsumerGroup("seckill-page-consumer-group");
//        RocketMQConsumer.setTopic("seckill-product-added-topic");
//
//        RocketMQConsumer.setListener(new MessageListenerConcurrently() {
//
//            public ConsumeConcurrentlyStatus consumeMessage(
//                    List<MessageExt> list,
//                    ConsumeConcurrentlyContext consumeConcurrentlyContext) {
//                for(MessageExt messageExt : list) {
//                    String seckillProductJSON = new String(messageExt.getBody());
//                    JSONObject seckillProductJSONObject = JSONObject.parseObject(seckillProductJSON);
//
//                    Long productId = seckillProductJSONObject.getLong("productId");
//                    Double seckillPrice = seckillProductJSONObject.getDouble("seckillPrice");
//                    Long seckillStock = seckillProductJSONObject.getLong("seckillStock");
//
//                    FreemarkerHelper viewEngine = new FreemarkerHelper();
//                    Map<String, Object> paras = new HashMap<String, Object>();
//                    paras.put("productId", productId);
//                    paras.put("seckillPrice", seckillPrice);
//                    paras.put("seckillStock", seckillStock);
//                    String html = viewEngine.parseTemplate("autolist.ftl", paras);
//
//                    System.out.println("将渲染完毕的秒杀商品html页面写入磁盘文件......");
//                    System.out.println(html);
//                    System.out.println("将磁盘上的html文件使用scp命令传送到nginx服务器上去......");
//                }
//                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
//            }
//
//        });
//
//        RocketMQConsumer.init();

        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(
                "seckill-page-consumer-group");
        consumer.setNamesrvAddr("localhost:9876");

        consumer.subscribe("seckill_product_added_topic", "*");

        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> messageExts, ConsumeConcurrentlyContext context) {
                for(MessageExt messageExt : messageExts) {
                    System.out.println(new String(messageExt.getBody()));
                    String seckillProductJSON = new String(messageExt.getBody());
                    JSONObject seckillProductJSONObject = JSONObject.parseObject(seckillProductJSON);

                    Long productId = seckillProductJSONObject.getLong("productId");
                    Double seckillPrice = seckillProductJSONObject.getDouble("seckillPrice");
                    Long seckillStock = seckillProductJSONObject.getLong("seckillStock");

                    FreemarkerHelper viewEngine = new FreemarkerHelper();
                    Map<String, Object> paras = new HashMap<String, Object>();
                    paras.put("productId", productId);
                    paras.put("seckillPrice", seckillPrice);
                    paras.put("seckillStock", seckillStock);
                    String html = viewEngine.parseTemplate("autolist.ftl", paras);

                    System.out.println("将渲染完毕的秒杀商品html页面写入磁盘文件......");
                    System.out.println(html);
                    System.out.println("将磁盘上的html文件使用scp命令传送到nginx服务器上去......");
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        consumer.start();
        System.out.println("消费者启动......");
    }
}
